<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="参数名">
              <a-input placeholder="" v-model="queryParam.paramKey"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-button @click="() => {this.queryPage()}" type="primary">查询</a-button>
            <a-button @click="() => queryParam = {}" style="margin-left: 8px">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <v-table
      bordered
      ref="table"
      :data="loadData"
      :columns="columns"
    >
      <template v-slot:buttons>
        <a-button @click="$refs.AddOrUpdate.init('','add')" icon="plus" type="primary" v-action="'sys:client:save'">增加参数</a-button>
      </template>
      <span slot="status" slot-scope="{row,text}">
        {{getstatus(text)}}
      </span>
      <span slot="action" slot-scope="{row,text}">
        <span v-action="'sys:config:update'">
          <a @click="edit(row)">修改</a>
          <a-divider type="vertical" />
        </span>
        <span v-action="'sys:config:del'">
          <a-popconfirm title="是否删除字典" @confirm="del(row)" okText="是" cancelText="否">
            <a-icon slot="icon" type="question-circle-o" style="color: red" />
            <a href="javascript:;" style="color: red">删除</a>
          </a-popconfirm>
        </span>
      </span>
    </v-table>
    <add-or-update
      @ok="queryPage"
      ref="AddOrUpdate"
    />
  </a-card>
</template>

<script>
import { fetchList, delObj } from '@api/sys/configUser'
import { TableMixin } from '@/mixins/TableMixin'
import { statusTypeList } from '@/dict'
import AddOrUpdate from './AddOrUpdate'
export default {
  name: 'Config',
  mixins: [TableMixin],
  components: {
    AddOrUpdate
  },
  data () {
    return {
      statusTypeList,
      columns: [
        {
          title: '用户',
          field: 'userName'
        },
        {
          title: '参数',
          field: 'paramKey'
        },
        {
          title: '参数值',
          field: 'paramValue'
        },
        {
          title: '状态',
          field: 'status',
          slot: true
        },
        {
          title: '备注',
          field: 'remark'
        },
        {
          title: '操作',
          field: 'action',
          slot: true
        }
      ],
      queryParam: {
        paramKey: ''
      },
      loadData: parameter => {
        return fetchList(
          Object.assign(parameter, this.queryParam)
        ).then(res => {
          return res.data
        })
      }
    }
  },
  methods: {
    getstatus (text) {
      const value = statusTypeList.map(item => {
        if (+item.value === +text) {
          return item.label
        }
      })
      return value[1]
    },
    edit (record) {
      this.$refs.AddOrUpdate.init(record.id, 'edit')
    },
    del (record) {
      delObj(record.id).then(res => {
        this.$message.info('删除成功')
        this.queryPage()
      })
    }
  }
}
</script>

<style scoped>

</style>
